package com.zzx.boot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzx.boot.common.R;
import com.zzx.boot.pojo.Category;
import com.zzx.boot.pojo.DishFlavor;
import com.zzx.boot.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/category")
@Slf4j
public class CategoryController {
//    填充注入
    @Resource
    private CategoryService service;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        boolean save = service.save(category);
        return  save ?R.success("添加成功!"):R.error("添加失败!");
    }

    /**
     * 分页功能
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize){
        R<Page> pageR = service.selectPage(page, pageSize);
        return pageR;
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){

        boolean b = service.updateById(category);
        return b?R.success("修改成功!"):R.error("修改失败!");
    }

    @DeleteMapping("")
    public R<String> delete(@RequestParam("ids") Long id){
      return service.remove(id);
    }

    @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        List<Category> typeList;
        if (type!=null){
          typeList = service.getTypeList(type);
     }else {
         typeList = service.list(null);
     }
        return R.success(typeList);
    }

}
